import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import { getToken } from '@/utils/auth'

const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 500000
})

service.interceptors.request.use(
  config => {
    config.url = decodeURI(encodeURI(config.url).replace(/%E2%80%8B/g, ''))
    if (getToken()) {
      const ahthToken = 'Bearer ' + getToken()
      config.headers['Authorization'] = ahthToken
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  response => {
    const res = response.data
    return res
  },
  error => {
    Message({
      message: error.response.data.msg,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
